//登录
app.controller("wxCheckCtrl", function ($scope, $state, $http, $ionicLoading) {
    var wx_code = localStorage.getItem("wx_code");
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    //进入页面初始化
    angular.element(document).ready(function () {
        //验证是否绑定过帐号
        $scope.checkLogin(wx_code);
    });
    $scope.checkLogin = function (wx_code) {
        var url = getPath.url+getPath.name+"/app/wxlogin";
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: {code:wx_code}
        }).success(function (data) {
                if (data.resultCode == 200) {
                    var res = data.resultBean;
                    //如果未绑定微信openId，跳转登录页使用用户名密码登录
                    //使用微信号做自动登录

                    var user = {
                        username: res.userWechatid,//微信号
                        password: "111",//密码，随便输入
                        openId: res.openid,//openId
                        headimgurl:res.headimgurl,
                    };

                    localStorage.setItem("wx_user",JSON.stringify(user));
                    //如果未绑定，跳转登录页进行绑定
                    if(res.regStatus==0){
                        $state.go("login");
                    }else{
                        var _url = getPath.url+getPath.name+"/app/a/loginByWx";
                        $http({
                            url: _url,
                            method: "POST",
                            headers:{'Content-Type':'application/x-www-form-urlencoded'},
                            data: user
                        }).success(function (data) {
                                if (data.resultCode == 200) {
                                    //登录成功，用户信息存到缓存中
                                    $ionicLoading.hide();
                                    var UserInfo = data.resultBean;
                                    user_info = UserInfo;
                                    //存放到缓存里
                                    localStorage.setItem("UserInfo",JSON.stringify(UserInfo));
                                    $state.go('home.today');
                                } else {
                                    $ionicLoading.show({
                                        template: "微信登录失败",
                                        duration: 3000
                                    });
                                }
                            })
                            .error(function () {
                                $ionicLoading.hide();
                                $ionicLoading.show({
                                    template: "登录失败",
                                    duration: 3000
                                });
                            });
                    }
                } else {
                    $ionicLoading.show({
                        template: "您的微信未经过授权",
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "获取信息失败",
                    duration: 3000
                });
            });
    }
});
